package com.yipao.admin.service.dev;

import com.yipao.admin.service.LbsTableService;
import com.yipao.core.util.ResponseUtil;
import com.yipao.db.domain.devinfo.DevInfo;
import com.yipao.db.service.devinfo.DevFormulaSlotService;
import com.yipao.db.service.devinfo.DevInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 管理端的设备管理
 *
 * @author tianhao
 * @date 2020/6/6 22:56
 **/
@Service
@Slf4j
public class AdminDevInfoService {
    @Resource
    private DevInfoService devInfoService;
    @Resource
    private LbsTableService lbsTableService;
    @Resource
    private DevFormulaSlotService devFormulaSlotService;

    /**
     * 新增设备
     *
     * @param devInfo
     * @return
     */
    public Object insert(DevInfo devInfo) {
        // 判断设备机器识别码是否存在
        if (devInfoService.queryDevCountByImei(devInfo.getImei()) > 0) {
            return ResponseUtil.fail(402, "机器识别码imei已存在");
        }
        int result = devInfoService.insert(devInfo);
        if (result > 0) {
            // 插入腾讯地图
            lbsTableService.insert(devInfo);
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

    /**
     * 删除
     *
     * @param id 设备id
     * @return
     */
    public Object delete(Integer id) {
        int count = devInfoService.delete(id);
        if (count > 0) {
            // 删除设备原料卡槽
            devFormulaSlotService.deleteDevSlotByDevId(id);
            // 删除腾讯地图
            lbsTableService.deleteById(id.toString());
            return ResponseUtil.ok("删除成功");
        }
        return ResponseUtil.ok("删除失败");
    }

    /**
     * 多条记录删除
     *
     * @param ids
     */
    public Object deleteIds(Integer[] ids) {
        for (Integer id : ids) {
            this.delete(id);
        }
        return ResponseUtil.ok();
    }

    /**
     * 修改
     *
     * @param devInfo
     * @return
     */
    public Object update(DevInfo devInfo) {
        if (StringUtils.isNotBlank(devInfo.getImei())) {
            Integer id = devInfoService.getIdByImei(devInfo.getImei());
            if (id != null &&
                    !devInfo.getId().equals(id)) {
                return ResponseUtil.fail(402, "机器识别码imei已存在");
            }
        }
        DevInfo oldDevInfo = devInfoService.selectByPrimaryKey(devInfo.getId());
        int result = devInfoService.update(devInfo);
        if (result > 0) {
            compareDevUpdateLbs(oldDevInfo, devInfo);
            return ResponseUtil.ok(devInfo);
        }
        return ResponseUtil.fail();
    }

    /**
     * 比对设备,如果不同,则修改腾讯云存储
     *
     * @param oldDevInfo
     * @param newDevInfo
     */
    @Async
    public void compareDevUpdateLbs(DevInfo oldDevInfo, DevInfo newDevInfo) {
        boolean isUpdated = false;
        String lat = newDevInfo.getLat();
        if (StringUtils.isNotBlank(lat)) {
            if (!oldDevInfo.getLat().equals(lat)) {
                isUpdated = true;
            }
        } else {
            newDevInfo.setLat(oldDevInfo.getLat());
        }
        String lng = newDevInfo.getLng();
        if (StringUtils.isNotBlank(lng)) {
            if (!oldDevInfo.getLng().equals(newDevInfo.getLng())) {
                isUpdated = true;
            }
        } else {
            newDevInfo.setLng(oldDevInfo.getLng());
        }
        String address = newDevInfo.getAddress();
        if (StringUtils.isNotBlank(address)) {
            if (!oldDevInfo.getAddress().equals(newDevInfo.getAddress())) {
                isUpdated = true;
            }
        } else {
            newDevInfo.setAddress(oldDevInfo.getAddress());
        }

        String scenes = newDevInfo.getScenes();
        if (StringUtils.isNotBlank(scenes)) {
            if (!oldDevInfo.getScenes().equals(newDevInfo.getScenes())) {
                isUpdated = true;
            }
        } else {
            newDevInfo.setScenes(oldDevInfo.getScenes());
        }

        if (isUpdated) {
            lbsTableService.update(newDevInfo);
        }
    }
}
